/*
 *  DOWANT.cpp
 *  TubeTruck
 *
 *  Created by Aeturnum on 6/4/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "DOWANT.h"

using namespace TTP;
using namespace Crypt;
using namespace std;

/*
std::vector<byte> mGroupSig, mSymKey mGroupPrivateKey;
*/

DOWANT::DOWANT() : Packet(Packet_DOWANT), mSymKey(), mGroupPrivateKey()
{}
DOWANT(id_hash idHash) : Packet(idHash, Packet_DOWANT)
{}
DOWANT::DOWANT(id_hash idHash, vector<byte>& groupSig, vector<byte>& symKey, vector<byte>& groupPrivateKey) :
Packet(idHash, Packet_DOWANT), Signable(groupSig), 
mSymKey(symKey), mGroupPrivateKey(groupPrivateKey)
{}
DOWANT::~DOWANT() {}

// accessor
vector<byte> DOWANT::getGroupSig() const {
   return getSigniture();
}
vector<byte> DOWANT::getSymmetricKey() const {
   return mSymKey;
}
vector<byte> DOWANT::getGroupPrivateKey() const {
   return mGroupPrivateKey;
}

void DOWANT::setGroupSig(Crypt::Signiture newSig) {
   setSigniture(newSig);
}

void DOWANT::setSymmetricKey(vector<byte>& symKey) {
   mSymKey = symKey;
}
void DOWANT::setGroupPrivateKey(vector<byte>& groupPrivateKey) {
   mGroupPrivateKey = groupPrivateKey;
}

// idHash
void DOWANT::setDestIdHash(id_hash idHash) {
   mIdHash = idHash;
}

id_hash DOWANT::getDestIdHash() const {
   return mIdHash;
}


//inherited
void DOWANT::fromVec(std::vector<byte>::const_iterator &vit, std::vector<byte>::const_iterator &end) {
   Packet::fromVec(vit, end);
   mSymKey.clear();
   mGroupPrivateKey.clear();
   mEncryptedSection.clear();
   
   mSig.fromVec(vit, end);
   mEncryptedSection.insert(mEncryptedSection.end(), vit, end);
   
   mEncrypted = true;
}

void DOWANT::toVec(vector<byte> &vec) const {
   if(!mEncrypted) {
      throw msg_exception("ERROR:toVec: DOWANT not encrypted!");
   } else {
      if(!mSigned) {
         throw msg_exception("ERROR:DOWANT not signed!");
      } else {
         Packet::toVec(vec);
         mSig.toVec(vec);
         vec.insert(vec.end(), mEncryptedSection.begin(), mEncryptedSection.end());
      }
   }
}

vector<byte> DOWANT::getEncryptedSection() {
   if(!mEncrypted) {
      fillEncryptedSection();
   }
   return mEncryptedSection;
      
}

void DOWANT::fillEncryptedSection() {
   if(mEncryptedSection.size() != mSymKey.size() + mGroupPrivateKey.size()) {
      mEncryptedSection.clear();
      mEncryptedSection.insert(mEncryptedSection.end(), mSymKey.begin(), mSymKey.end());
      mEncryptedSection.insert(mEncryptedSection.end(), mGroupPrivateKey.begin(), mGroupPrivateKey.end());
   }
}

void DOWANT::setEncryptedData(vector<byte>& encData) {
   mEncryptedSection = encData;
   mEncrypted = true;
}

void DOWANT::setPlainText(vector<byte>& unencData) {
   mSymKey.clear();
   mGroupPrivateKey.clear();
   
   if(unencData.size() != Crypt::RSA::RSAKeyLength + 16)
      throw msg_exception("Plaintext Incorrect size!");
   
   mSymKey.insert(mSymKey.end(), unencData.begin(), unencData.begin()+16);
   mGroupPrivateKey.insert(mGroupPrivateKey.end(), unencData.begin()+16, unencData.end());
   
   fillEncryptedSection();
}

vector<byte> DOWANT::getUnsignedData() const{
   if(!mEncrypted) {
      throw msg_exception("ERROR:getUnsignedData: DOWANT not encrypted!");
   } else {
      vector<byte> dataToSign;
      vector<byte> emptySig;
      
      Packet::toVec(dataToSign);
      emptySig.insert(emptySig.end(), mSig.size(), 0);
      dataToSign.insert(dataToSign.end(), emptySig.begin(), emptySig.end());
      dataToSign.insert(dataToSign.end(), mEncryptedSection.begin(), mEncryptedSection.end());
      return dataToSign;
   }
}